home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GSMAIN.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  9KB  |  290 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsmain.c */
  20. /* Common support for Ghostscript front ends */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "gp.h"
  25. #include "gsmatrix.h"            /* for gxdevice.h */
  26. #include "gxdevice.h"
  27. #include "gserrors.h"
  28. #include "estack.h"
  29. #include "ostack.h"            /* put here for files.h */
  30. #include "stream.h"            /* for files.h */
  31. #include "files.h"
  32. #include "iscan.h"
  33. #include "main.h"
  34. #include "store.h"
  35.  
  36. /* ------ Exported data ------ */
  37.  
  38. uint gs_memory_chunk_size = 20000;
  39. int gs_user_errors;
  40.  
  41. /* File name search paths */
  42. const char **gs_lib_paths;
  43. private int gs_lib_count;
  44. char *gs_lib_env_path;
  45.  
  46. /* ------ Imported data ------ */
  47.  
  48. /* Configuration information imported from gconfig.c. */
  49. extern gx_device *gx_device_list[];
  50. extern const char *gs_lib_default_path;
  51. extern const char *gs_init_file;
  52. extern ref gs_init_file_array[];
  53.  
  54. /* Imported from gsmisc.c */
  55. extern int gs_log_errors;
  56. extern FILE *gs_debug_out;
  57.  
  58. /* ------ Forward references ------ */
  59.  
  60. private int gs_run_init_file(P2(int *, ref *));
  61. private int gs_run_file_open(P2(const char *, ref *));
  62.  
  63. /* ------ Initialization ------ */
  64.  
  65. /* Remember how much initialization has been done. */
  66. private int init1_done, init2_done;
  67.  
  68. /* A handy way to declare and execute an initialization procedure: */
  69. #define call_init(proc)\
  70. { extern void proc(P0()); proc(); }
  71.  
  72. /* Initialization to be done before anything else. */
  73. void
  74. gs_init0(FILE *in, FILE *out, FILE *err, int max_lib_paths)
  75. {    /* Set the Ghostscript versions of stdin/out/err. */
  76.     gs_stdin = in;
  77.     gs_stdout = out;
  78.     gs_stderr = err;
  79.     gs_debug_out = gs_stdout;
  80.     /* Do platform-dependent initialization. */
  81.     /* We have to do this as the very first thing, */
  82.     /* because it detects attempts to run 80N86 executables (N>0) */
  83.     /* on incompatible processors. */
  84.     gp_init();
  85.     /* Initialize the file search paths */
  86.     gs_lib_env_path = 0;
  87.     gs_lib_paths =
  88.         (const char **)gs_malloc(max_lib_paths + 3, sizeof(char *),
  89.                      "gs_lib_paths array");
  90.     gs_lib_count = 0;
  91.     gs_user_errors = 1;
  92.     gs_log_errors = 0;
  93.     /* Reset debugging flags */
  94.     memset(gs_debug, 0, 128);
  95.     init1_done = init2_done = 0;
  96. }
  97.  
  98. /* Initialization to be done before constructing any objects. */
  99. void
  100. gs_init1(void)
  101. {    if ( !init1_done )
  102.        {    alloc_init(&gs_default_memory_procs, gs_memory_chunk_size);
  103.         call_init(name_init)
  104.         call_init(obj_init)        /* requires name_init */
  105.         call_init(scan_init)        /* ditto */
  106.         init1_done = 1;
  107.        }
  108. }
  109.  
  110. /* Initialization to be done before running any files. */
  111. void
  112. gs_init2(void)
  113. {    gs_init1();
  114.     if ( !init2_done )
  115.        {    int code, exit_code;
  116.         ref error_object;
  117.         call_init(igs_init)
  118.         call_init(zop_init)
  119.         interp_init(1);        /* requires obj_init */
  120.         call_init(op_init)    /* requires obj_init, scan_init */
  121.         /* Set up the array of additional initialization files. */
  122.         {    ref *ifp = gs_init_file_array;
  123.             ref ifa;
  124.             for ( ; ifp->value.bytes != 0; ifp++ )
  125.               r_set_size(ifp, strlen((const char *)ifp->value.bytes));
  126.             make_tasv(&ifa, t_array, a_readonly,
  127.                   ifp - gs_init_file_array, refs,
  128.                   gs_init_file_array);
  129.             initial_enter_name("INITFILES", &ifa);
  130.         }
  131.         /* Execute the standard initialization file. */
  132.         code = gs_run_init_file(&exit_code, &error_object);
  133.         if ( code < 0 )
  134.         {    if ( code != gs_error_Fatal )
  135.                 gs_debug_dump_stack(code, &error_object);
  136.             gs_exit_with_code((exit_code ? exit_code : 2), code);
  137.         }
  138.         init2_done = 1;
  139.        }
  140.    }
  141.  
  142. /* Add a library search path to the list. */
  143. void
  144. gs_add_lib_path(const char *lpath)
  145. {    gs_lib_paths[gs_lib_count] = lpath;
  146.     gs_lib_count++;
  147.     gs_set_lib_paths();
  148. }
  149.  
  150. /* ------ Execution ------ */
  151.  
  152. /* Complete the list of library search paths. */
  153. void
  154. gs_set_lib_paths(void)
  155. {    const char **ppath = &gs_lib_paths[gs_lib_count];
  156.     if ( gs_lib_env_path != 0 ) *ppath++ = gs_lib_env_path;
  157.     if ( gs_lib_default_path != 0 ) *ppath++ = gs_lib_default_path;
  158.     *ppath = 0;
  159. }
  160.  
  161. /* Open a file, using the search paths. */
  162. int
  163. gs_lib_open(const char *file_name, ref *pfile)
  164. {    /* This is a separate procedure only to avoid tying up */
  165.     /* extra stack space while running the file. */
  166. #define maxfn 200
  167.     byte fn[maxfn];
  168.     uint len;
  169.     return lib_file_open(file_name, strlen(file_name), fn, maxfn,
  170.                  &len, pfile);
  171. }
  172.  
  173. /* Open and execute a file. */
  174. int
  175. gs_run_file(const char *file_name, int user_errors, int *pexit_code, ref *perror_object)
  176. {    ref initial_file;
  177.     int code = gs_run_file_open(file_name, &initial_file);
  178.     if ( code < 0 ) return code;
  179.     return gs_interpret(&initial_file, user_errors, pexit_code, perror_object);
  180. }
  181. private int
  182. gs_run_file_open(const char *file_name, ref *pfref)
  183. {    gs_set_lib_paths();
  184.     if ( gs_lib_open(file_name, pfref) < 0 )
  185.     {    eprintf1("Can't find initialization file %s.\n", file_name);
  186.         return_error(gs_error_Fatal);
  187.     }
  188.     r_set_attrs(pfref, a_execute + a_executable);
  189.     return 0;
  190. }
  191.  
  192. /* Open and run the very first initialization file. */
  193. private int
  194. gs_run_init_file(int *pexit_code, ref *perror_object)
  195. {    ref initial_file;
  196.     ref first_token;
  197.     int code;
  198.     const char *file_name = gs_init_file;
  199.     /* Check to make sure the first token is an integer */
  200.     /* (for the version number check.) */
  201.     code = gs_run_file_open(file_name, &initial_file);
  202.     if ( code < 0 )
  203.         return code;
  204.     code = scan_token(initial_file.value.pfile, 0, &first_token);
  205.     if ( code < 0 || !r_has_type(&first_token, t_integer) )
  206.     {    eprintf1("Initialization file %s is not compatible with this interpreter.\n", file_name);
  207.         *pexit_code = 255;
  208.         return_error(gs_error_Fatal);
  209.     }
  210.     *++osp = first_token;
  211.     return gs_interpret(&initial_file, gs_user_errors, pexit_code, perror_object);
  212. }
  213.  
  214. /* Run a string. */
  215. int
  216. gs_run_string(const char *str, int user_errors, int *pexit_code, ref *perror_object)
  217. {    ref fref;
  218.     int code;
  219.     gs_set_lib_paths();
  220.     code = file_read_string((const byte *)str, (uint)strlen(str), &fref);
  221.     if ( code < 0 ) return code;
  222.     r_set_attrs(&fref, a_executable);
  223.     code = gs_interpret(&fref, user_errors, pexit_code, perror_object);
  224.     file_close(&fref);
  225.     return code;
  226. }
  227.  
  228. /* ------ Termination ------ */
  229.  
  230. /* Free all resources and exit. */
  231. void gs_malloc_release(P0());
  232. int gs_closedevice(P1(gx_device *));
  233. void
  234. gs_finit(int exit_status, int code)
  235. {    gx_device **pdev = gx_device_list;
  236.     fflush(stderr);            /* in case of error exit */
  237.     for ( ; *pdev != 0; pdev++ )
  238.         gs_closedevice(*pdev);
  239.     /* Do the equivalent of a restore "past the bottom". */
  240.     /* This will release all memory, close all open files, etc. */
  241.     if ( init1_done )
  242.         alloc_restore_all();
  243.     /* Do platform-specific cleanup. */
  244.     gp_exit(exit_status, code);
  245. }
  246. void
  247. gs_exit_with_code(int exit_status, int code)
  248. {    gs_finit(exit_status, code);
  249.     exit(exit_status);
  250. }
  251. void
  252. gs_exit(int exit_status)
  253. {    gs_exit_with_code(exit_status, 0);
  254. }
  255.  
  256. /* ------ Debugging ------ */
  257.  
  258. /* Debugging code */
  259. extern void debug_print_ref(P1(const ref *));
  260. extern void debug_dump_refs(P3(const ref *, uint, const char *));
  261.  
  262. /* Dump the stacks after interpretation */
  263. void
  264. gs_debug_dump_stack(int code, ref *perror_object)
  265. {    zflush(osp);    /* force out buffered output */
  266.     dprintf1("\nUnexpected interpreter error %d.\n", code);
  267.     if ( perror_object !=